# -*- coding: utf-8 -*-

def index():
    return dict()

def auth_test():
    " 由 start/binduser/ 代替"
#    auth_test = sip.auth.getSession()
    auth_url = taobao_api.get_url(login_keys_list, session.sip_dict, SIP_LOGIN_URL, False)
    return dict(
        auth_url = auth_url,
        )

@check_session
def bought_test():
    bought = sip.taobao.trades.bought.get(fields = ['title', 'iid', 'tid'])
    response.flash = '掌柜 '+str(session.isp_username)+' 的买入记录'
    return dict(
        bought = bought,
        )
        
def taobao_products_test():
    products = sip.taobao.products.search(fields = ['product_id', 'props', 'props_str', 'cat_name', 'name', 'sale_props_str', 'binds_str', 'price', 'modified', 'pic_path'], q='pc')
    return dict(
        products = products,
        )
        
def ali_items_test():
#    sip_dict = sip.__dict__

#    appInstanceId = session.sip_dict['appInstanceId']
#    token = session.sip_dict['token']
#    userId = session.sip_dict['userId']
#
#    validateUser = sip.alisoft.validateUser(appInstanceId, userId, token)
#    validateAppUser  = sip.alisoft.validateAppUser(appInstanceId, userId)

    items_get = sip.taobao.items.get(fields=['iid','title','nick','pic_path','cid','price','type','location.city','delist_time','post_fee'], q='笔记本', page_no='121', start_price='199', end_price='1900', ww_status='true', page_size='5', order_by='price:desc')
    iid_get = sip.taobao.item.get(fields=['iid','title','nick','pic_path','cid','price','type','location.city','delist_time','post_fee'], iid='62988750ce0769570d7567e12d4f156b', nick='绿匣子数码旗舰店')
#    items_get = sip.taobao.items.get.location(city='北京')
#    items_instock_get = sip.taobao.items.instock.get(fields=['iid', 'title', 'nick'], q='nike',)
    
    return dict(
#        sip_dict = sip_dict,
#        validateUser = validateUser,
#        validateAppUser = validateAppUser,

        items_get = items_get,
        iid_get = iid_get
#        items_instock_get = items_instock_get,
        )

def db_test():
    form = [i.api for i in db().select(db.interface.ALL)]
    return dict(
        form = form,
        )

def start_test():
    if not session.user_name:
        if session.sip_dict:
            sip_dict = session.sip_dict
            sip_dict['sip_apiname'] = 'alisoft.validateUser'
            validate_user_url = taobao_api.get_url(validate_user_keys_list, sip_dict, sip_sign=True)
            validate_user_rsp = fetch(validate_user_url)
            validate_user_xml = minidom.parseString(validate_user_rsp)
#            # temp list
#            session.SIP = sip.alisoft.validateUser(appId=sip_dict['appId'], appInstanceId=sip_dict['appInstanceId'],userId=sip_dict['userId'],token=sip_dict['token'])

            if validate_user_xml.getElementsByTagName("String")[0].firstChild.nodeValue == '1':
                sip_dict['sip_apiname'] = 'alisoft.udb.getDomainLoginId'
                get_domain_login_id_url = taobao_api.get_url(get_domain_login_id_keys_list, sip_dict, sip_sign=True)
                get_domain_login_id_rsp = fetch(get_domain_login_id_url)
                get_domain_login_id_xml = minidom.parseString(get_domain_login_id_rsp)
#                get_domain_login_id_json = simplejson.loads(get_domain_login_id_rsp)
                session.user_name = get_domain_login_id_xml.getElementsByTagName("result")[0].firstChild.nodeValue
#                session.user_name = get_domain_login_id_json
                redirect(URL(r=request, c = 'default'))
        else:
            redirect(URL(r=request, c = 'default'))
    else:
        redirect(URL(r=request, c = 'default'))

#    sip_login_url = taobao_api.sip_url(login_keys_list, sip_dict)
#    sip_login_url = SIPLOGIN_URL + '?' + sip_login_url
#
#    validate_user_sign = taobao_api.sip_sign(validate_user_keys_list, sip_dict)
#    validate_user_url = taobao_api.sip_url(validate_user_keys_list, sip_dict)
#    validate_user_url = SIP_REST_URL + '?' + validate_user_url + 'sip_sign=' + validate_user_sign

    return dict(
        )

def images_test():
    form = SQLFORM(db.images)  
    return dict(
        form = form,
        )

def jiajia():
    db.jiajia.insert(time = request.now, title=request.function)
    form = db().select(db.jiajia.ALL)
    return dict(
        form = form,
        )

def jiajia2():
    db.jiajia.insert(time = request.now, title=request.function)
    form = db().select(db.jiajia.ALL)
    return dict(
        form = form,
        )

def gethttp():
    httpv = response.status
    return dict(
        httpv = httpv
        )

def each_api():
    rsp = sip.alisoft.im.online(uid=userId)
    return dict(
        rsp = rsp,
        )

def args_test():
    i = '哩哩啦啦'
    a = A('a', _href=URL(r=request, args=['lll','pkpkp','fieieifei','wwwwwwwwwwwww','wiiwiwiwiwiw',i]))
    x = request.args
    
    return dict(
        a = a,
        x = x,
        )

def product_get():
    rsp = sip.taobao.product.get(fields='name, desc', product_id=10864884,)
    return dict(rsp = rsp)

def trades_get():
#    rsp = sip.taobao.trades.get(seller_nick='肖安娜',fields=['title'],iid='10864884')
    rsp = sip.taobao.trades.get(seller_nick='吴恩泽鞋王',fields=['title','price','num','created','type','buyer_nick'],iid='5e22f603661060a3a27706e511829a3d')
    return dict(rsp = rsp)

def items_get():
    v = sip.taobao.items.get(fields=['title','desc','pic_path','delist_time','price','auto_repost'], q='实体店')
    return dict(v = v)

def items_test():
    pass

def other():
    session['other'] = sip
    jj = session['jj'] = session['other'].taobao.product.get(fields='name, desc', product_id=10864884,)
    
    return dict(
        jj = jj
        )
